package com.ourteam.product.model.api;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractServiceController;
import com.ourteam.frame.web.AbstractSearchHelper;
import com.ourteam.frame.web.GridBean;
import com.ourteam.product.model.dao.ProductDatabaseDataTypeMapDAOQueryBean;
import com.ourteam.product.model.domain.ProductDatabaseDataTypeMapBean;
import com.ourteam.product.model.service.IProductModelService;
import com.ourteam.product.model.service.ProductModelServiceFactory;

@Controller
@CrossOrigin
@RequestMapping("/api/PRODUCT/MODEL/productDatabaseDataTypeMap")
public class ProductDatabaseDataTypeMapAPI extends AbstractServiceController {

	private static final IProductModelService PRODUCT_MODEL_SERVICE = ProductModelServiceFactory
			.getProductModelService();

	@RequestMapping("/queryDataTypeMapList")
	@ResponseBody
	public GridBean queryDataTypeMaps(@ModelAttribute ProductDataTypeMapSearchModel searchModel,
			HttpServletRequest request, Model model) throws Exception {

		AbstractSearchHelper<ProductDatabaseDataTypeMapDAOQueryBean, ProductDataTypeMapSearchModel> searchHelper = new AbstractSearchHelper<ProductDatabaseDataTypeMapDAOQueryBean, ProductDataTypeMapSearchModel>(
				searchModel) {

			@Override
			protected int queryCount(ProductDatabaseDataTypeMapDAOQueryBean queryBean) throws Exception {
				return PRODUCT_MODEL_SERVICE.queryProductDatabaseDataTypeMapCount(queryBean);
			}

			@Override
			protected Object[] queryData(ProductDatabaseDataTypeMapDAOQueryBean queryBean) throws Exception {
				return PRODUCT_MODEL_SERVICE.queryProductDatabaseDataTypeMaps(queryBean);
			}

			@Override
			protected ProductDatabaseDataTypeMapDAOQueryBean createQueryBean() {
				return new ProductDatabaseDataTypeMapDAOQueryBean();
			}

		};

		return searchHelper.doSearch(request);

	}

	@RequestMapping("/deleteDataTypeMap")
	@ResponseBody
	public boolean deleteDataTypeMap(@RequestParam(value = "ids") long[] ids) throws Exception {

		PRODUCT_MODEL_SERVICE.deleteProductDatabaseDataTypeMaps(ids);

		return true;
	}

	@RequestMapping("/getDataTypeMapById")
	@ResponseBody
	public ProductDatabaseDataTypeMapBean getDataTypeMapById(@RequestParam long id) throws Exception {

		return PRODUCT_MODEL_SERVICE.getProductDatabaseDataTypeMapById(id);

	}

	@RequestMapping("/saveDataTypeMap")
	@ResponseBody
	public ProductDatabaseDataTypeMapBean saveDataTypeMap(
			@ModelAttribute ProductDatabaseDataTypeMapBean dataTypeMapBean) throws Exception {

		if (dataTypeMapBean.getId() == 0) {
			PRODUCT_MODEL_SERVICE.addNewProductDatabaseDataTypeMap(dataTypeMapBean);
		} else {
			PRODUCT_MODEL_SERVICE.modifyProductDatabaseDataTypeMap(dataTypeMapBean);
		}

		return dataTypeMapBean;
	}

}
